package org.jeecg.modules.rental.util;

import lombok.RequiredArgsConstructor;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.app.api.AppApi;
import org.jeecg.modules.app.constant.AppConstant;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户端菜单权限缓存
 */
@Component
@RequiredArgsConstructor
public class AppMenuUtil {

    private final RedisUtil redisUtil;

    private final AppApi appApi;

    public List<String> appClientMenus(String appId){
        List<String> menusList = new ArrayList<>();
        String menusListKey = AppConstant.APP_CLIENT_MENUS_CACHE_KEY + "::" + appId;
        if (redisUtil.hasKey(menusListKey)) {
            menusList = (List<String>) redisUtil.get(menusListKey);
        }else{
            menusList = appApi.menusList(appId);
            if(oConvertUtils.isNotEmpty(menusList)){
                //保存到redis
                redisUtil.set(menusListKey, menusList);
            }
        }
        return menusList;
    }

}
